package com.chinawlx.wlxteacher.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.network.WLXHttpRxHelper;
import com.chinawlx.wlxteacher.network.bean.WLXScheduleScoreBean;
import com.chinawlx.wlxteacher.ui.adapter.WLXTeachIdeaAdapter;
import com.chinawlx.wlxteacher.utils.WLXDateUtil;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXLogUtil;
import com.chinawlx.wlxteacher.utils.WLXTeacherConstant;
import com.chinawlx.wlxteacher.wlx_schedule_score;
import com.chinawlx.wlxteacher.wlx_schedule_scoreDao;
import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by wlx on 16/10/14.
 */

public class WLXTeachIdeaActivity extends WLXBaseActivity {
    @BindView(R.id.recylerview)
    RecyclerView mRecyclerView;
    private List<wlx_schedule_score> wlxScheduleScores;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teachidea);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        iniData();
        initView();
    }

    private void initView() {

    }

    private void iniData() {
        String resourceId = getIntent().getStringExtra(WLXTeacherConstant.BAD_SCHEDULE_ID);
        WLXHttpRxHelper.getInstance().getBadList(resourceId, new Subscriber<WLXScheduleScoreBean>
                () {
            @Override
            public void onCompleted() {
                initDataForDataBase();
            }

            @Override
            public void onError(Throwable e) {
                onCompleted();
            }

            @Override
            public void onNext(WLXScheduleScoreBean scoreBean) {
                saveScoreTable(scoreBean);
            }
        });
    }

    private void initDataForDataBase() {
        wlxScheduleScores = WLXGreenDaoUtil.getScheduleScoreDao()
                .queryBuilder().list();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(new WLXTeachIdeaAdapter(this,wlxScheduleScores));
        WLXLogUtil.printE("wlxScheduleScores"+wlxScheduleScores.size());
    }

    /**
     * 存储wlx_grade_notice表
     *
     * @param scoreBean
     */
    private void saveScoreTable(WLXScheduleScoreBean scoreBean) {
        if (scoreBean.getCode() == 0) {
            Gson gson = new Gson();
            List<WLXScheduleScoreBean.DataBean.ScoreListBean> scoreList = scoreBean
                    .getData().getScore_list();
            for (WLXScheduleScoreBean.DataBean.ScoreListBean scoreListBean : scoreList) {
                String schedule_score_id = Integer.toString(scoreListBean.getId());
                int user_id = scoreListBean.getUser_id();
                int family_user_id = scoreListBean.getFamily_user_id();
                int schedule_id = scoreListBean.getSchedule_id();
                int grade_id = scoreListBean.getGrade_id();
                String content = scoreListBean.getContent();
                String schedule_score_code = scoreListBean.getSchedule_score().getKey();
                String extend_info = gson.toJson(scoreListBean.getExtend());
                long creation_date = WLXDateUtil.getStringToDate(scoreListBean
                        .getCreation_date());
                //------------------存储wlx_grade_notice表-----------------
                wlx_schedule_score wlx_schedule_score = new wlx_schedule_score
                        (schedule_score_id, user_id, family_user_id, schedule_id,
                                grade_id, content, schedule_score_code, extend_info,
                                creation_date);
                List<com.chinawlx.wlxteacher.wlx_schedule_score> wlx_schedule_scores =
                        WLXGreenDaoUtil.getScheduleScoreDao().queryBuilder().where
                                (wlx_schedule_scoreDao
                                        .Properties.Schedule_score_id.eq(schedule_score_id)).list();
                if (wlx_schedule_scores.size() > 0) {
                    if (creation_date > wlx_schedule_scores.get(0)
                            .getCreation_date()) {
                        WLXGreenDaoUtil.getScheduleScoreDao().update(wlx_schedule_score);
                    }
                } else {
                    WLXGreenDaoUtil.getScheduleScoreDao().insert(wlx_schedule_score);
                }
            }
        }
    }

    @OnClick({R.id.iv_back_teachidea})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back_teachidea:
                finish();
                break;
        }
    }
}
